package com.ror.game;

import com.ror.domain.Player;
import com.ror.event.CardDistributedEventArgs;
import com.ror.event.GameEvent;
import com.ror.event.GameObserver;
import com.ror.util.AssetsUtil;

import javax.swing.*;
import java.awt.*;

public class GameFrame extends JFrame implements GameObserver {
    //游戏核心逻辑模块
    public final PokerGame core = new PokerGame(this);

    private final JLabel lordLabel = new JLabel(new ImageIcon(AssetsUtil.LORD_ICON_PATH));
    private final JTextField[] timerTextFields = new JTextField[3];
    private Container contentPane;

    public GameFrame() {
        initFrame();
        initView();
        this.setVisible(true);
        core.initGame();
    }

    //添加组件
    public void initView() {
        initBtns();
        initTimersUI();
        initLordLabel();
    }

    //初始化地主组件，隐藏且不设位置
    private void initLordLabel() {
        lordLabel.setSize(40, 40);
        lordLabel.setVisible(false);
        contentPane.add(lordLabel);
    }

    //添加3名玩家前方的倒计时组件
    private void initTimersUI() {
        //每个玩家一个，左边的电脑玩家是0，中间的自己是1，右边的电脑玩家是2
        for (int i = 0; i < 3; i++) {
            timerTextFields[i] = new JTextField("倒计时:");
            timerTextFields[i].setEditable(false);
//            timerTextFields[i].setVisible(false);
            contentPane.add(timerTextFields[i]);
        }
        timerTextFields[0].setBounds(140, 230, 60, 20);
        timerTextFields[1].setBounds(380, 350, 60, 20);
        timerTextFields[2].setBounds(620, 230, 60, 20);
    }

    //添加按钮
    private void initBtns() {
        //叫地主、抢地主按钮
        JButton bidBtn = new JButton("叫地主");
        bidBtn.setBounds(320, 380, 90, 40);
        bidBtn.addActionListener(Player.thisPlayer::bid);
//        bidBtn.setVisible(false);
        contentPane.add(bidBtn);

        //不抢按钮
        JButton noBidBtn = new JButton("不抢");
        noBidBtn.setBounds(410, 380, 90, 40);
        noBidBtn.addActionListener(Player.thisPlayer::noBid);
//        noBidBtn.setVisible(false);
        contentPane.add(noBidBtn);

        //出牌按钮
        JButton playBtn = new JButton("出牌");
        playBtn.setBounds(320, 420, 90, 40);
        playBtn.addActionListener(Player.thisPlayer::play);
//        playBtn.setVisible(false);
        contentPane.add(playBtn);

        //不要按钮
        JButton passBtn = new JButton("不要");
        passBtn.setBounds(410, 420, 90, 40);
        passBtn.addActionListener(Player.thisPlayer::pass);
//        passBtn.setVisible(false);
        contentPane.add(passBtn);
    }

    //初始化界面
    public void initFrame() {
        //设置标题
        this.setTitle("斗地主");
        this.setSize(830, 620);
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        contentPane = this.getContentPane();
        contentPane.setLayout(null);
        contentPane.setBackground(Color.LIGHT_GRAY);
    }

    @Override
    public void onGameUpdated(GameEvent e) {
        switch (e.getType()) {
            case COMPONENT_UPDATED:
                //组件更新时，重绘内容面板
                contentPane.repaint();
                break;
            case CARD_DISTRIBUTED:
                //发牌时，初始化一张扑克牌组件
                CardDistributedEventArgs args = (CardDistributedEventArgs) e.getData();
                initPokerLabel(args);
                break;
        }
    }

    //初始化一张扑克牌组件
    private void initPokerLabel(CardDistributedEventArgs args) {
        PokerLabel label = new PokerLabel(args.card);
        contentPane.add(label);
        //设置叠放方式为后来居上
        contentPane.setComponentZOrder(label, 0);
        //让组件自身继续处理这个事件
        label.onCardDistributed(args);
    }
}
